package com.me.jingdongshop.ui.fragment;

import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.BaseFragment;
import com.me.common.base.Const;
import com.me.common.base.Global;
import com.me.common.util.SharedPreUtil;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Cart;
import com.me.jingdongshop.model.bean.ProFavo;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.TestFragmentPre;
import com.me.jingdongshop.ui.activity.SettlementActivity;
import com.me.jingdongshop.ui.adapter.ShopcartAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YL on 2017/5/4.
 */
public class ShopCartFragment extends BaseFragment {
    private ListView lv;
    private ImageButton ibBack;     // 返回按钮
    private TextView tvTitle;       // 顶部标题
    private TextView tvTitleNum;    // 标题数字
    private ImageView ivCheckAll;   // 选择全部
    private TextView tvTotal;        // 合计
    private ImageButton ibAccounts;  // 结算
    private List<ProFavo.ProductListBean> productList;
    private TestFragmentPre testFragmentPre;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_shopcart;
    }

    @Override
    public void initView() {
        lv = findView(R.id.lv);
//        lv.setAdapter(new ShopcartAdapter(getActivity()));

        ibBack = findView(R.id.ib_back);
        tvTitle = findView(R.id.tv_title);
        tvTitleNum = findView(R.id.tv_title_num);
        ivCheckAll = findView(R.id.iv_check_all);
        tvTotal = findView(R.id.tv_total);
        ibAccounts = findView(R.id.ib_accounts);
    }

    @Override
    public void initListener() {
        ivCheckAll.setOnClickListener(this);
        ibBack.setOnClickListener(this);
        ibAccounts.setOnClickListener(this);
    }

    @Override
    public void initData() {
        testFragmentPre = new TestFragmentPre(this);
        testFragmentPre.postCart(1,3,"1,2,3,4");

    }
    List<List<Cart.CartBean>> cartList=new ArrayList<>();
    @Override
    public void onClick(View v, int id) {
        switch (id) {
            case R.id.ib_back:
                Toast.makeText(getContext(), "返回", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_check_all:
                Toast.makeText(getContext(), "点击全选", Toast.LENGTH_SHORT).show();
                ivCheckAll.setImageResource(R.drawable.ischeck);
                break;
            case R.id.ib_accounts:
                String uid = SharedPreUtil.getString(Global.mContext, Const.KEY_UID,"");
                if(TextUtils.isEmpty(uid)){
                    Toast.makeText(mActivity, "请登录再结算", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(mActivity, SettlementActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType == IHttpService.HTTP_FAVO){
            ProFavo bean = (ProFavo) msg.obj;
            productList = bean.getProductList();
            for (int i = 0; i < productList.size(); i++) {
                int id = productList.get(i).getId();

                testFragmentPre.postCart(id,3,"2,2");
            }
        }

        if (reqType == IHttpService.HTTP_CART){
             Cart bean = (Cart) msg.obj;
            List<Cart.CartBean> cart = bean.getCart();
            cartList.add(cart);
            ShopcartAdapter adapter = new ShopcartAdapter(Global.mContext,cartList);
            lv.setAdapter(adapter);
//            Cart.CartBean.ProductBean product = cart.get(0).getProduct();
//            String name = product.getName();

        }
    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
